package cn.nubia.admin.application.service;

import cn.nubia.admin.application.service.FirewareService;
import cn.nubia.admin.application.service.SoftService;
import cn.nubia.admin.application.bean.ClientVersion;
import cn.nubia.admin.application.query.ClientQuery;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.nubia.admin.application.bean.ClientVO;

/**
 * Created by justin on 15/7/11.
 */
@Service
public class ClientService extends BaseServiceImpl<ClientVersion> {
    @Autowired
    SoftService softService;
    @Autowired
    FirewareService firewareService;
    @Autowired
    SystemLogService systemLogService;

    /***
     * 保存或更新客户端信息
     * @param vo
     */
    public void processSaveClient(ClientVO vo) {
        Integer curr = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        if(vo.getId()==null){
            ClientVersion clientVersion = new ClientVersion();
            BeanUtils.copyProperties(vo, clientVersion);
            clientVersion.setId(null);
            clientVersion.setState(vo.getSubmitType());
            clientVersion.setCreateAdminId(adminId);
            clientVersion.setCreateTime(curr);
            clientVersion.setLastModifyTime(curr);
            if(clientVersion.getState()== ClientVersion.STATE_UP){
                clientVersion.setPublishTime(curr);
            }
            insert(clientVersion);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.CLIENTLIST, SystemLog.OperateTypeEnum.ADD, clientVersion.getId());
        }else{
            ClientVersion clientVersionDomain = get(vo.getId());
            ClientVersion clientVersion = new ClientVersion();
            BeanUtils.copyProperties(vo, clientVersion);
            clientVersion.copyStakeProperties(clientVersionDomain);
            clientVersion.setState(vo.getSubmitType());
            if(clientVersion.getState()== ClientVersion.STATE_UP){
                clientVersion.setPublishTime(curr);
            }
            clientVersion.setLastModifyTime(curr);
            update(clientVersion);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.CLIENTLIST, SystemLog.OperateTypeEnum.EDIT, clientVersion.getId());
        }

    }

    /****
     * 查询客户端
     * @param clientQuery
     * @return
     */
    public Paged<ClientVersion> listClientVersions(ClientQuery clientQuery) {
        Query<ClientVersion> query = Query.build(ClientVersion.class);
        query.addNotEq(ClientVersion.COLUMN_STATE, ClientVersion.STATE_DEL);

        if(clientQuery.getChannelId()!=null&&clientQuery.getChannelId()>0){
            query.addEq(ClientVersion.COLUMN_CHANNELID,clientQuery.getChannelId());
        }

        if(clientQuery.getClientType()!=null&&clientQuery.getClientType()>=0){
            query.addEq(ClientVersion.COLUMN_CLIENTTYPE,clientQuery.getClientType());
        }

        if(clientQuery.getPublishs()!=null&&clientQuery.getPublishe()!=null&
                clientQuery.getPublishs()!=null&&clientQuery.getPublishe()!=null){
            query.addBetween(ClientVersion.COLUMN_PUBLISHTIME, DateUtil.getIntegerTime(clientQuery.getPublishs())
                    , DateUtil.getIntegerTime(clientQuery.getPublishe()));
        }

        query.addOrder(ClientVersion.COLUMN_PUBLISHTIME, Query.DBOrder.DESC);
        query.addOrder(ClientVersion.COLUMN_CREATETIME, Query.DBOrder.DESC);
        query.setPaged(clientQuery.getPageNo(), clientQuery.getPageSize());
        Paged<ClientVersion> paged = findPagedByQuery(query);
        for(ClientVersion cv : paged.getListData() ){
            cv.setPublishTimeShow(DateUtil.getFormatDate(cv.getPublishTime()));
        }
        return paged;
    }


    /****
     * 获取ClientVO
     * @param id
     * @return
     */
    public ClientVO getClientVersion(Integer id) {
        ClientVersion clientVersion = get(id);
        ClientVO vo = new ClientVO(clientVersion);
        if(vo.getResolution()>0){
            vo.setResolutionShow(softService.getResolutionShow(vo.getResolution()));
        }
        vo.setMaxsdkName(firewareService.getFirewareNameByFirewareSDK(vo.getMaxsdk()));
        vo.setMinsdkName(firewareService.getFirewareNameByFirewareSDK(vo.getMinsdk()));
        return vo;
    }

    /**
     *
     * @param id
     * @return
     */
    public boolean disableClientVersion(Integer id) {
        ClientVersion clientVersion=get(id);
        clientVersion.setState(ClientVersion.STATE_DEL);
        update(clientVersion);
        //log
        systemLogService.log(SystemLog.ModuleTypeEnum.CLIENTLIST, SystemLog.OperateTypeEnum.DEL, id);
        return true;
    }

    public boolean changeState(Integer id, int state) {
        ClientVersion clientVersion=get(id);
        clientVersion.setState(state);
        update(clientVersion);
        if(state == ClientVersion.STATE_UP){
            systemLogService.log(SystemLog.ModuleTypeEnum.CLIENTLIST, SystemLog.OperateTypeEnum.UP, id);
        }
        return true;
    }
}
